/**
 * Created with IntelliJ IDEA.
 * User: ajhin
 * Date: 1/31/14
 * Time: 3:10 PM
 * To change this template use File | Settings | File Templates.
 */
package com.societyGames.codeAlong.state.codeExercise.view.testCase
{
import com.societyGames.flashForms.IChangeSize;
import com.societyGames.flashForms.ItemsContainer;
import com.societyGames.system.EventObject;

import flash.display.DisplayObject;

public class TestCaseButtonsContainer extends ItemsContainer implements IChangeSize
{
  private var _sizeChangedEventObject:EventObject;
  private var _spacingWidth:Number = 0;
  private var _spacingHeight:Number = 0;

  public function TestCaseButtonsContainer()
  {
    _sizeChangedEventObject = new EventObject(this, "SIZE_CHANGED");
  }

  public function set spacingWidth(value:Number):void
  {
    _spacingWidth = value;
    calculate();
  }

  public function get spacingWidth():Number
  {
    return _spacingWidth;
  }

  public function set spacingHeight(value:Number):void
  {
    _spacingHeight = value;
    calculate();
  }

  public function get spacingHeight():Number
  {
    return _spacingHeight;
  }

  //Make all of the items in _items appear correctly.
  protected override function calculate():void
  {
    var tileWidthToHeight:Number = 1;
    var tileHeightToWidth:Number = 1;

    var tileHeight:Number;
    var tileWidth:Number;
    var tileCount:int = _items.length;
    var totalWidthMinusOutsideSpacing:Number; //Doesn't count outer spacing

    tileWidth = (_desiredWidth - ((tileCount + 1) * _spacingWidth)) / tileCount; //Try full width.
    tileHeight = tileHeightToWidth * tileWidth;
    totalWidthMinusOutsideSpacing = (tileCount * tileWidth) + ((tileCount - 1) * _spacingWidth);

    var maxTileHeight:Number = _desiredHeight - (_spacingHeight * 2);
    if (tileHeight > maxTileHeight) //Full height is too wide! So full wide must be smaller.
    {
      tileHeight = maxTileHeight; //Try with full height.
      tileWidth = tileWidthToHeight * tileHeight;       //Set tile width using full height.
      totalWidthMinusOutsideSpacing = (tileCount * tileWidth) + ((tileCount - 1) * _spacingWidth);
    }

    //If we went negative, just zero everything.
    if (tileWidth < 0 || tileHeight < 0)
    {
      tileWidth = 0;
      tileHeight = 0;
    }

    //Now walk the items and set their x, y, height, width
    var currentX:Number = (_desiredWidth - totalWidthMinusOutsideSpacing) / 2;
    var currentY:Number = (_desiredHeight - tileHeight) / 2;
    for each (var child:DisplayObject in _items)
    {
      child.x = currentX;
      child.y = currentY;
      child.width = tileWidth;
      child.height = tileHeight;
      currentX += tileWidth + _spacingWidth;
    }

    this._sizeChangedEventObject.dispatchEvent();
  }

  protected override function calculateHeight():void
  {
    calculate();
  }

  protected override function calculateWidth():void
  {
    calculate();
  }

  //IChangeSize-->
  public function get sizeChangedEventObject():EventObject
  {
    return _sizeChangedEventObject;
  }

  //<--IChangeSize
}
}